package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;


import ModelLayer.Clothing;

public class DBClothing {
	private static Connection con;
	Statement stmt;
	public DBClothing() 
	{
		con = DbConnection.getInstance().getDBcon();
	}
	public void createClothing(String size,String color)
	{
		try{
			int clID=GetMax.getMaxId("Select max(clID) from Clothing")+1;//Gets the maximum clID from the DB and adds+1 for the next ID
			String query ="INSERT INTO Clothing(clID, size, color)  VALUES('"+clID+"','"+size+"','"+color+"')";
			 Statement stmt = con.createStatement();
	          stmt.setQueryTimeout(5);
	     	  stmt.executeUpdate(query);
	          stmt.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			}
	}
	public void deleteClothing(int clID)
	{	//clID=1; 
		try{
			String query="DELETE FROM Clothing WHERE clID = '"+clID+"'";
			Statement stmt = con.createStatement();
	          stmt.setQueryTimeout(5);
	     	  stmt.executeUpdate(query);
	          stmt.close();
		}
		catch(Exception e)
		{e.printStackTrace();
		}
	}
	public void updateClothing(Clothing g)
	{ Clothing suObj = g;
	String query="UPDATE Clothing SET "+
		 	  "size ='"+ suObj.getSize()+"', "+
		 	  "color ='"+ suObj.getColor() + "'" +
		          " WHERE clID = '"+ suObj.getClID() + "'";
              System.out.println("Update query:" + query);
              try{
            	  Statement stmt = con.createStatement();
            	  stmt.setQueryTimeout(5);
            	  stmt.executeUpdate(query);
            	  stmt.close();
            	  
              }
              catch(Exception e)
              {e.printStackTrace();}
		
	}
	
	private String buildQuery()
	{
	    String query="SELECT clID,size,color  FROM Clothing";
		/*
		if (wClause.length()>0)
			query=query+" WHERE ";
			*/
		return query;
	}
	public ArrayList<Clothing> miscWhere()
	{
            ResultSet results;
	    ArrayList<Clothing> list = new ArrayList<Clothing>();	
		
	    String query =  buildQuery();
  
            try{ // read the employee from the database
		Statement stmt = con.createStatement();
	 	stmt.setQueryTimeout(5);
	 	results = stmt.executeQuery(query);
	 	
	
		while( results.next() ){
	     	 Clothing empObj = new Clothing();
		         empObj = buildClothing(results);	
                 list.add(empObj);	
		}//end while
                 stmt.close();     
                 
			
		}//slut try	
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	public Clothing findClothing(int clID)
	{
		ResultSet results;
		Clothing empObj = new Clothing();
                
	        String query =  buildQuery() + " WHERE clID ="+"'"+clID+"'";
                System.out.println(query);
		try{ // 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
                            empObj = buildClothing(results);
                            //assocaition is to be build
                            stmt.close();
                            
                            
			}
                        else{ //no employee was found
                            empObj = null;
                        }
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return empObj;
	}
	private Clothing buildClothing(ResultSet results)
    {   Clothing suObj = new Clothing();
        try{ // the columns from the table glayee  are used
              suObj.setClID(results.getInt("clID"));
              suObj.setSize(results.getString("size"));
              suObj.setColor(results.getString("address"));

              
        }
       catch(Exception e)
       {
           System.out.println("error in building the Clothing object");
       }
       return suObj;
}
	
}